//右滑关闭
mui.init({
	swipeBack: true,
})
var counts = "";
var UserNO = localStorage.getItem("UserNO");
// 储存临时计算的库存条码
var StockList = []; // 每个元素格式: { BarCodeNO: "条码编号" }

mui.plusReady(function() {
	GetSingOrder();
	$("#BarCode").focus();
	plus.nativeUI.closeWaiting();
})


//初始化显示
function GetSingOrder() {
	WarehouseList();
	$("#BarCode").keyup(function(event) {
		event.preventDefault();
		// 检查是否按下了回车键（keyCode 13）
		if (event.keyCode == 13) {
			var BarCode = $("#BarCode").val();
			if (BarCode.indexOf("||") !== -1) { // 或者用 includes("||")
				BarCode = BarCode.split("||")[0];
				//console.log("解析到的条码:", firstBarcode); 
			}
			$("#BarCode").val(BarCode);
			// 发送请求
			xccFormPostAsync("api/PDA/QueryMaterialInfo", {
				BarcodeNO: BarCode

			}, function(data) {
				document.activeElement.blur();
				if (data.Code === 200) {
					$("#MaterialCode").val(data.Items[0].MaterialNO);
					$("#BarCode").val('');
					$("#Qty").focus();

				}
				plus.nativeUI.closeWaiting(); // 关闭转圈圈特效
			}, function(error) {
				$("#BarCode").val('');
				$("#BarCode").focus();
				plus.nativeUI.closeWaiting(); // 关闭转圈圈特效
			});

			$("#MaterialCode").focus();
		}
	});

	$("#all").html("");
	$("#all").html(
		'<a style="font-size:100px; color:#F27D48; display:block; text-align:center;" class="mui-icon mui-icon-search"></a><a class="mui-title" style="color:#808080; position: initial; margin: 0 auto; height:' +
		(window.screen.height - 480) + 'px">No data available at the moment</a>');
	StockList = [];
}

//亮灯
function LigthUP() {
	var ColorNO = $("#shelfOption").val();
	var datas = {
		Color: ColorNO,
		LigthCheck: true,
		Items: StockList
	};
	xccFormPostAsync("api/PDA/QueryLigthInfo", datas, function(data) {
		if (data.Code === 200) {
			mui.toast("success ", {
				duration: '1000',
				type: 'div'
			});
		} else {
			mui.toast("error " + data.Message, {
				duration: '1000',
				type: 'div'
			});
		}
	}, function(errr) {})
}

//灭灯
function LigthOff() {
	var ColorNO = $("#shelfOption").val();
	var datas = {
		ColorNO: ColorNO,
		LigthCheck: false,
		Items: StockList
	};
	xccFormPostAsync("api/PDA/QueryLigthInfo", datas, function(data) {
		if (data.Code === 200) {
			mui.toast("success ", {
				duration: '1000',
				type: 'div'
			});
		} else {
			mui.toast("error " + data.Message, {
				duration: '1000',
				type: 'div'
			});
		}
	}, function(errr) {})
}


// 初始化仓库
function WarehouseList() {
	xccpostAsync("api/PDA/GetWarehouseInfo", {}, function(data) {
		document.activeElement.blur();
		console.log(JSON.stringify(data));
		if (data.Code === 200) {
			data.Items.forEach(item => {
				$("#WarehouseList").append(
					`<option value="${item.WarehouseID}">${item.WarehouseName}</option>`
				);
			});
			$("#BarCode").focus();
		} else {
			mui.toast("failed。" + data.Message, {
				duration: '1000',
				type: 'div'
			});
		}
		plus.nativeUI.closeWaiting(); // 关闭转圈圈特效
	}, function(error) {
		console.log("没有数据....");
		plus.nativeUI.closeWaiting(); // 关闭转圈圈特效
	});
}

//条码检索
function GetStockList() {
	var html = "";
	var MaterialNO = $("#MaterialCode").val();
	if (!MaterialNO) {
		mui.toast("The material number is empty", {
			duration: '1000',
			type: 'div'
		});
		return false;
	}
	var Qty = $("#Qty").val();
	if (!Qty) {
		mui.toast("The Qty is empty ", {
			duration: '1000',
			type: 'div'
		});
		return false;
	}
	xccFormPostAsync("api/PDA/GetStockCodeList", {
		MaterialNO: MaterialNO,
		Qty: Qty,
		WarehouseID: $("#WarehouseList").val()
	}, function(data) {
		if (data.Items != null && data.Items.length > 0) {
			$("#all").html(""); ////全部清空

			for (var i = 0; i < data.Items.length; i++) {
				var existingIndex = StockList.findIndex(item => item.BarcodeNO === data.Items[i].BarcodeNO);

				if (existingIndex === -1) {
					StockList.push({
						BarcodeNO: data.Items[i].BarcodeNO,
						LampNO: data.Items[i].LampNO,
						LampCode: data.Items[i].LampCode,
						StorageNO: data.Items[i].StorageNO,
						StockQty: data.Items[i].StockQty
					});
				};
				var itemClass = data.Items[i].IsLock == 1 ? 'locked-item' : '';

				$("#all").append(
					`<li class="mui-table-view-cell ${itemClass}">
				                    <div class="list-title">Code:${data.Items[i].BarcodeNO}&nbsp;&nbsp;&nbspQty:${data.Items[i].StockQty}</div>
				                    <div class="list-detail">LampNO:${data.Items[i].LampNO}&nbsp;&nbsp;&nbspStrNO:${data.Items[i].StorageNO}</div></li>`
				);
			}

		}
	}, function(errr) {
		$("#all").html("");
		$("#all").html(
			'<a style="font-size:100px; color:#F27D48; display:block; text-align:center;" class="mui-icon mui-icon-search"></a><a class="mui-title" style="color:#808080; position: initial; margin: 0 auto; height:' +
			(window.screen.height - 480) + 'px">No data available at the moment</a>');
		StockList = [];
	})
}